package com.spa.interfaces.admin;

import cn.dev33.satoken.annotation.SaIgnore;
import com.spa.application.command.admin.*;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.SysUserDto;
import com.spa.application.query.admin.AdminUserDetailsQuery;
import com.spa.application.query.admin.AdminUserQuery;
import com.spa.application.service.admin.AdminSysUserApplicationService;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.infrastructure.mapper.SysUserMapperStruct;
import com.spa.infrastructure.util.SessionUtil;
import com.spa.interfaces.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("admin/user")
public class AdminUserController extends BaseController {

    @Resource
    private AdminSysUserApplicationService adminSysUserApplicationService;

    @PostMapping("add")
    @SaIgnore
    public ResponseMessage add(@Validated @RequestBody AdminUserAddCommand command) {
        adminSysUserApplicationService.addUser(command);
        return responseHelper.success();
    }

    @PostMapping("delById")
    public ResponseMessage delById(@Validated @RequestBody AdminUserDelCommand command) {
        return responseHelper.success(adminSysUserApplicationService.delUser(command));
    }

    @PostMapping("edit")
    @SaIgnore
    public ResponseMessage edit(@Validated @RequestBody AdminUserEditCommand command) {
        adminSysUserApplicationService.editUser(command);
        return responseHelper.success();
    }

    @PostMapping("page")
    public ResponseMessage<PageResult<SysUserDto>> page(@Validated @RequestBody AdminUserQuery query) {
        return responseHelper.success(adminSysUserApplicationService.pageUser(SysUserMapperStruct.INSTANCE.query2Command(query)));
    }

    @PostMapping("detailsById")
    public ResponseMessage<SysUserDto> detailsById(@Validated @RequestBody AdminUserDetailsQuery query) {
        return responseHelper.success(adminSysUserApplicationService.detailsById(query.getUserId()));
    }

    @PostMapping("myDetails")
    public ResponseMessage<SysUserDto> myDetails() {
        return responseHelper.success(adminSysUserApplicationService.detailsById(SessionUtil.getUser().getExtUserId()));
    }

}
